package controller

import (
	"bugManagementPlatform/pojo"
	"bugManagementPlatform/service"
	"bugManagementPlatform/tools"
	jsoniter "github.com/json-iterator/go"
	"github.com/savsgio/atreugo/v11"
	"strconv"
)

type BugManagementController struct {
	BugManagementService *service.BugManagementService
	UserService *service.UserService
}

func NewBugManagementController() *BugManagementController{
	return &BugManagementController{
		BugManagementService: service.NewBugManagementService(),
		UserService: service.NewUserService(),
	}
}

func (bmc *BugManagementController) QueryFiledAll(ctx *atreugo.RequestCtx) error {

	pageNoStr, _ := ctx.UserValue("pageNo").(string)
	pageSizeStr, _ := ctx.UserValue("pageSize").(string)
	idStr, _ := ctx.UserValue("id").(string)
	name := string(ctx.FormValue("name"))
	moldName := string(ctx.FormValue("moldName"))
	mold := string(ctx.FormValue("mold"))
	startTime := string(ctx.FormValue("startTime"))
	endTime := string(ctx.FormValue("endTime"))

	pageNo, _ := strconv.Atoi(pageNoStr)
	pageSize, _ := strconv.Atoi(pageSizeStr)
	id, _ := strconv.ParseInt(idStr, 10, 64)

	res, total := bmc.BugManagementService.QueryFiledAll(pageNo, pageSize, id, name, moldName, mold, startTime, endTime)

	ctx.Response.Header.Set("status ", "200")

	return ctx.JSONResponse(map[string]interface{}{"code": 200, "message": "成功", "data":map[string]interface{}{"rows":res, "total":total}})
}

func (bmc *BugManagementController) AddFiled(ctx *atreugo.RequestCtx) error {

	name := string(ctx.FormValue("name"))
	mold, _ := strconv.Atoi(string(ctx.FormValue("mold")))
	moldName := string(ctx.FormValue("moldName"))
	picture := string(ctx.FormValue("picture"))

	//bmc.BugManagementService.AddFiled(name, mold, moldName, picture)

	res, _ := tools.TransactionDecorator(bmc.BugManagementService.AddFiled, name, mold, moldName, picture)

	ctx.Response.Header.Set("status ", "200")

	//return ctx.JSONResponse(map[string]interface{}{"code": 200, "message": "成功", "data":nil})

	return ctx.JSONResponse(res)
}

func (bmc *BugManagementController) UpdateFiled(ctx *atreugo.RequestCtx) error {

	var rf pojo.ResField
	rfByte := ctx.PostBody()
	dErr := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(rfByte, &rf)
	if dErr != nil{
		return ctx.JSONResponse(map[string]interface{}{"code":500, "message":dErr.Error()})
	}

	bmc.BugManagementService.UpdateFiled(&rf)

	ctx.Response.Header.Set("status ", "200")

	return ctx.JSONResponse(map[string]interface{}{"code": 200, "message": "成功", "data":nil})
}

func (bmc *BugManagementController) DeleteFiledById(ctx *atreugo.RequestCtx) error {

	id, _ := strconv.ParseInt(string(ctx.FormValue("id")), 10, 64)

	bmc.BugManagementService.DeleteFiledById(id)

	ctx.Response.Header.Set("status ", "200")

	return ctx.JSONResponse(map[string]interface{}{"code": 200, "message": "成功", "data":nil})
}

func (bmc *BugManagementController) Login(ctx *atreugo.RequestCtx) error {

	var u pojo.User
	uByte := ctx.PostBody()
	dErr := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(uByte, &u)
	if dErr != nil{
		return ctx.JSONResponse(map[string]interface{}{"code":500, "message":dErr.Error()})
	}

	res,et := bmc.UserService.Login(&u)
	if res != ""{
		ctx.Response.Header.Set("status ", "200")
		return ctx.JSONResponse(map[string]interface{}{"code": 200, "message": "登录成功", "data":map[string]interface{}{"token":res, "expiresAt":et}})
	}else {
		ctx.Response.Header.Set("status ", "400")
		return ctx.JSONResponse(map[string]interface{}{"code": 400, "message": "账号或密码错误", "data":nil})
	}
}

func(bmc *BugManagementController)ViewReportById(ctx *atreugo.RequestCtx) error {

	id, _ := strconv.ParseInt(string(ctx.FormValue("id")), 10, 64)

	ctx.SendFile(bmc.BugManagementService.ViewReportById(id))

	return nil
}







